package net.alche.fa.core.doing;
import net.alche.fa.core.model.Logic;
import net.alche.fa.core.model.FaValue;
import net.alche.fa.core.CodeLevel;
import net.alche.fa.core.SageStone;
import net.alche.fa.core.FaException;

//
// for
//
@FaAnnoDoing
@SuppressWarnings("all") 
public class PlusPlusDo extends AbstractDoing {

  @Override
  public boolean matched(String what, Logic g) {
    return "++".equals(what) || "--".equals(what);
  }

  @Override
  public FaValue doing(Logic g, CodeLevel lvl, SageStone st) throws FaException {
    FaValue fav;
    if (g.getLogicData() == null && g.getLogicObj() == null) {
      fav = st.getVarData(g.getObj(), lvl);
    }
    else {
      fav = super.getLogicObjVal(st, lvl, g);
    }
    return this.doPlusPlus(g, fav);
  }

  // 执行++ / --
  public static FaValue doPlusPlus(Logic g, FaValue fav) throws FaException {
    FaValue backVal = new FaValue(fav.getIntValue());
    if (g.getWhat().equals("++")) {
      fav.setIntValue(fav.getIntValue() + 1);
    }
    else if ( g.getWhat().equals("--")) {
      fav.setIntValue(fav.getIntValue() - 1);
    }
    return backVal;
  }
}